package com.company.cloud.club.modules.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

//@Configuration
public class CorsConfig {
    //    @Value("${buffge.cors.origins}")
    private final String[] allowOrigins = new String[]{"http://localhost:3000", "http://localhost:3001"};

    /**
     * <p>Description：配置允许跨域访问</p>
     */
    private CorsConfiguration buildConfig() {
        CorsConfiguration conf = new CorsConfiguration();
        conf.setAllowCredentials(true);
        Arrays.stream(allowOrigins).forEach(conf::addAllowedOrigin);
        conf.addAllowedOrigin("*");
        conf.addAllowedHeader("*");
        conf.addAllowedMethod("*");
        return conf;
    }

    /**
     * <p>Description：跨域过滤器</p>
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }

}